package com.itheima.countroller;

import com.itheima.cammon.CommonResult;
import com.itheima.cammon.PageInof;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import com.itheima.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王同学
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //菜品分类
    @PostMapping
    public CommonResult add(@RequestBody Category category) {
        CategoryVO add = categoryService.add(category);

        return CommonResult.success(add);
    }

    @PutMapping
    public CommonResult update(@RequestBody Category category) {
        categoryService.update(category);
        return CommonResult.success("修改成功");
    }

    @DeleteMapping("")
    public CommonResult delete(String id) {
        boolean res = categoryService.delete(id);
        if (res == false) {
            return CommonResult.error("删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @GetMapping("/page")             //Request Param请求参数也就是设置默认值，当没有参数传进来的时候提供的一个默认值
    public CommonResult page(
            @RequestParam(defaultValue = "1") Integer start,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        //分页查询
        PageInof<Category> res = categoryService.page(start, pageSize);
        return CommonResult.success(res);
    }

    //分类下拉表
    @GetMapping("list")
    public CommonResult findAll(Integer type) {
        return CommonResult.success(categoryService.findAll(type));
    }

}
